package com.gl.organigrama.vos
{
	import com.gl.organigrama.events.GLOrganigramaEvent;
	
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	import mx.events.CollectionEvent;
	
    [RemoteClass( alias="com.gl.organigrama.vos.Nodo" )]	
	[Bindable]
	public class Nodo extends EventDispatcher
	{
		public var dispatcher:IEventDispatcher;
		
		public static const BORDO:String = "Bordo"
		public static const PINK:String = "Pink"
		public static const YELLOW :String= "Yellow"
		
		
		public var id:Number;
		public var cargo:String;
		public var sigla:String;
		public var estilo:String = PINK;
		public var nivel:Number = 1;
		
		[Bindable(event="nivelRealChanged")]
		public function get nivelReal():Number
		{
			return ( parent ? parent.nivelReal + nivel : 1 );
		}
		public function set nivelReal(value:Number):void
		{
			nivel = value - parent.nivelReal; 
			updateNivelReal();
			updateNodo();
		}
		
		public function updateNivelReal():void
		{
			dispatchEvent(new Event("nivelRealChanged"));
			notificarCambioNivelHijos();
		}
		
		private function notificarCambioNivelHijos():void
		{	
			for each( var nodoHijo:Nodo in hijosNivelUno )
			{
				nodoHijo.updateNivelReal();
			}
		}	
		
		private var _hijosNivelUno:ArrayCollection = new ArrayCollection();
		[Transist]
		public var parent:Nodo
		
//		private var _hijosNivelDos:ArrayCollection = new ArrayCollection();
		private var _hijosNivelDos:ArrayCollection = new ArrayCollection();
		
		public function get hijosNivelDos():ArrayCollection
		{
		  return _hijosNivelDos ;	
		}

		public function set hijosNivelDos(value:ArrayCollection):void
		{
			_hijosNivelDos = value;
			for each (var search:Nodo in _hijosNivelDos){
				Nodo(search).parent = this;
			}	
				
			_hijosNivelDos.addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionChange);
		}

		
		public function get hijosNivelUno():ArrayCollection
		{
			return _hijosNivelUno ;	
		}
		
		public function set hijosNivelUno(value:ArrayCollection):void
		{
			_hijosNivelUno = value;
			for each (var search:Nodo in _hijosNivelUno){
				Nodo(search).parent = this;
			}
			
			_hijosNivelUno.addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionChange);
			
		}
		private function collectionChange( collectionEvent:CollectionEvent ):void
		{
			if(collectionEvent.kind == "add" ||collectionEvent.kind == "remove" )
			{	
				for each (var search:Nodo in ArrayCollection(collectionEvent.currentTarget) ){
					search.parent = this;
					search.updateNivelReal();
				}
				updateNodo();
			}	
		}
		private function updateNodo():void
		{	
			var event:GLOrganigramaEvent = new GLOrganigramaEvent(GLOrganigramaEvent.ACTUALIZAR_NODO);
			event.nodo = this;
			event.nodoPadre = parent;
			FlexGlobals.topLevelApplication.dispatchEvent( event );
		}
		
		public var funcionario:Funcionario;
		public var esNodoBase:Boolean;
		
		public function Nodo()
		{
		}
	}
}